package com.dtda.edu.miaosha.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dtda.edu.miaosha.bo.ItemBo;
import com.dtda.edu.miaosha.bo.MarketBo;
import com.dtda.edu.miaosha.common.ValidatorImpl;
import com.dtda.edu.miaosha.common.ValidatorUtil;
import com.dtda.edu.miaosha.common.exception.BusinessException;
import com.dtda.edu.miaosha.common.response.EmBusinessError;
import com.dtda.edu.miaosha.dao.ItemPoMapper;
import com.dtda.edu.miaosha.dao.ItemSalePoMapper;
import com.dtda.edu.miaosha.dao.ItemStockPoMapper;
import com.dtda.edu.miaosha.po.ItemPo;
import com.dtda.edu.miaosha.po.ItemSalePo;
import com.dtda.edu.miaosha.po.ItemStockPo;
import com.dtda.edu.miaosha.service.ItemService;
import com.dtda.edu.miaosha.service.MarketService;

/**
 * @author 袁强
 * @version 2019年2月18日 下午4:35:54
 * 类说明  
 */
@Service("itemService")
public class ItemServiceImpl implements ItemService {

	@Autowired
	private ItemPoMapper itemPoMapper;
	@Autowired
	private ItemSalePoMapper itemSalePoMapper;
	@Autowired
	private ItemStockPoMapper itemStockPoMapper;
	@Autowired
	private MarketService marketService;
	
	
	//写入操作--->事务    创建商品
	@Transactional
	public ItemBo createTitle(ItemBo itemBo) throws BusinessException{
		// TODO Auto-generated method stub
		//校验入参
		ValidatorImpl validatorImpl = ValidatorUtil.validate(itemBo);
		if(validatorImpl.isHasError) {
			throw new BusinessException(EmBusinessError.PARAM_VALIDATION_ERROR, validatorImpl.getErrorMsg());
		}
		
		//转换ItemBo--->po
		ItemPo itemPo = new ItemPo();
		ItemSalePo itemSalePo = new ItemSalePo();
		ItemStockPo itemStockPo = new ItemStockPo();
		
		BeanUtils.copyProperties(itemBo, itemPo);  //BeanUtils 只会copy 类型一致的 属性
		itemPo.setPrice(itemBo.getPrice().doubleValue());
		//写入数据库
		itemPoMapper.insert(itemPo);  
		ItemPo insItemPo = itemPoMapper.selectByTitle(itemPo.getTitle());
		itemSalePo.setTitleSales(itemBo.getSales());
		itemSalePo.setTitleId(insItemPo.getId());
		itemSalePoMapper.insert(itemSalePo);
		itemStockPo.setTitleStock(Integer.valueOf(itemBo.getStock()));
		itemStockPo.setTitleId(insItemPo.getId());
		itemStockPoMapper.insert(itemStockPo);
		//返回创建完成的对象
		return this.showItemById(insItemPo.getId());
	}

	/**
	 * 展示所有的商品
	 */
	public List<ItemBo> showItems() {
		// TODO Auto-generated method stub
		//查询的商品以销量排序  [如果将销量放在商品表中可以通过 order by  .. desc 实现排序]
		ArrayList<ItemBo> items = new ArrayList<ItemBo>();
		List<ItemPo> itemPos = itemPoMapper.selectItems();
		for (ItemPo itemPo : itemPos) {
			ItemSalePo itemSalePo = itemSalePoMapper.selectByItemId(itemPo.getId());
			ItemStockPo itemStockPo = itemStockPoMapper.selectByItemId(itemPo.getId());
			ItemBo itemBo = new ItemBo();
			BeanUtils.copyProperties(itemPo, itemBo);
			itemBo.setPrice(BigDecimal.valueOf(itemPo.getPrice()));
			itemBo.setSales(itemSalePo.getTitleSales());
			itemBo.setStock(itemStockPo.getTitleStock());
			items.add(itemBo);
		}
		return items;
	}

	/**
	 * 通过商品id展示商品,并查询是否包含活动
	 */
	public ItemBo showItemById(Integer id) {
		// TODO Auto-generated method stub
		ItemPo itemPo = itemPoMapper.selectByPrimaryKey(id);
		if(itemPo == null) {
			return null;
		}
		ItemSalePo itemSalePo = itemSalePoMapper.selectByItemId(itemPo.getId());
		ItemStockPo itemStockPo = itemStockPoMapper.selectByItemId(itemPo.getId());
		MarketBo marketBo = marketService.selMarketByItemId(itemPo.getId());
		ItemBo itemBo = new ItemBo();
		BeanUtils.copyProperties(itemPo, itemBo);
		itemBo.setPrice(BigDecimal.valueOf(itemPo.getPrice()));
		if(itemSalePo == null) {
			itemBo.setSales(0);
		}
		itemBo.setSales(itemSalePo.getTitleSales());
		if(itemStockPo == null) {
			itemBo.setStock(0);
		}
		itemBo.setStock(itemStockPo.getTitleStock());
		if(marketBo != null ) { 
			itemBo.setMarketBo(marketBo);
		}
		return itemBo;
	}

	/**
	 * 修改库存
	 */
	@Transactional
	public boolean decreaseStock(Integer itemId, Integer itemAmount) throws BusinessException {
		// TODO Auto-generated method stub
		//这个方法的参数是否加注解
		int row = itemStockPoMapper.updateStockByAmount(itemId, itemAmount);
		if(row > 0) {
			//更新库存成功
			return true;
		}else {
			//更新库存失败
			return false;
		}
	}

	@Override
	@Transactional
	public boolean updateSales(Integer itemId, Integer itemAmount) throws BusinessException {
		// TODO Auto-generated method stub
		int row = itemSalePoMapper.updateTitleSales(itemId,itemAmount);
		if(row > 0) {
			//销量增加成功
			return true;
		}else {
			//销量增加失败
			return false;
		}
	}

	/**
	 * 删除商品:保证销量表/库存表记录都要删除
	 */
	@Override
	@Transactional
	public boolean deleteItem(Integer id) {
		// TODO Auto-generated method stub
		//外键的原因，所以先删除引用表
		itemSalePoMapper.deleteByTitleId(id);
		itemStockPoMapper.deleteByTitleId(id);
		int i = itemPoMapper.deleteByPrimaryKey(id);
		if(i > 0) {
			//marketService.deleteByTitleId(id);
			return true;
		}else {
			return false;
		}
	}

	@Override
	@Transactional
	public boolean updateItem(ItemBo itemBo) {
		// TODO Auto-generated method stub
		ItemPo itemPo = new ItemPo();
		BeanUtils.copyProperties(itemBo, itemPo);
		itemPo.setPrice(itemBo.getPrice().doubleValue());
		int i = itemPoMapper.updateByPrimaryKeySelective(itemPo);
		ItemStockPo itemStockPo = new ItemStockPo();
		itemStockPo.setTitleId(itemBo.getId());
		itemStockPo.setTitleStock(itemBo.getStock());
		int j = itemStockPoMapper.updateItem(itemStockPo);
		if(i>0 && j>0) {
			return true;
		}else {
			return false;
		}
	}
}
